package controller;

import java.util.List;

import javax.faces.context.FacesContext;

import model.Item;
import dao.DAOException;
import dao.ItemDAO;

public class ItemController {
	private ItemDAO dao = ItemDAO.getInstance();
	private Item item;
	
	public ItemController() {
		this.item = new Item();
	}
	
	public Item getItem() {
		return item;
	}
	public void setItem(Item item) {
		this.item = item;
	}
	
	public List<Item> getItens() {
		List<Item> itens = null;
		try {
			itens = dao.getAll();
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return itens;
	}

	public void savar() {
		try {
			dao.save(item);
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		item = new Item();
	}
	
	public void editar() {
		Integer id = Integer.parseInt((String) FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id"));
		try {
			item = dao.getById(id);
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void deletar(){
		Integer id = Integer.parseInt((String) FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id"));
		try {
			dao.delete(id);
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
